package com.xci.platform.auth.core;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.xci.platform.helper.JsonHelper;
import com.xci.platform.helper.StringHelper;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * 数据权限设置
 */
@Data
public class AuthDataSetting {
    /**
     * 所有
     */
    private boolean all;

    /**
     * 所在部门
     */
    private boolean department;

    /**
     * 所在部门及下级
     */
    private boolean departmentAndLower;

    /**
     * 仅本人
     */
    private boolean self;

    /**
     * 自定义
     */
    private boolean custom;

    /**
     * 自定义数据
     */
    private List<String> customData = new ArrayList<>();

    public AuthDataSetting() {
    }

    public AuthDataSetting(boolean all, boolean department, boolean departmentAndLower, boolean self,boolean custom) {
        this.all = all;
        this.department = department;
        this.departmentAndLower = departmentAndLower;
        this.self = self;
        this.custom = custom;
    }

    public static AuthDataSetting All                   = new AuthDataSetting(true,  false, false, false, false);
    public static AuthDataSetting Department            = new AuthDataSetting(false, true,  false, false, false);
    public static AuthDataSetting DepartmentAndLower    = new AuthDataSetting(false, false, true,  false, false);
    public static AuthDataSetting Self                  = new AuthDataSetting(false, false, false, true,  false);
    public static AuthDataSetting Custom                = new AuthDataSetting(false, false, false, false, true);
    public static AuthDataSetting None                  = new AuthDataSetting(false, false, false, false, false);

    /**
     * 合并数据配置
     *
     * @param dataSettings 数据配置数组
     */
    @JsonIgnore
    public AuthDataSetting merge(List<String> dataSettings) {
        List<AuthDataSetting> dataObjectSettings = new ArrayList<>();
        for (String item : dataSettings) {
            if (StringHelper.isBlank(item)) continue;
            AuthDataSetting isetting = JsonHelper.deserialize(item, AuthDataSetting.class);
            if (isetting == null) continue;
            dataObjectSettings.add(isetting);
        }
        return mergeObject(dataObjectSettings);
    }

    /**
     * 合并数据配置
     *
     * @param dataObjectSettings 数据配置数组
     */
    @JsonIgnore
    public AuthDataSetting mergeObject(List<AuthDataSetting> dataObjectSettings) {
        AuthDataSetting current = new AuthDataSetting();
        for (AuthDataSetting item : dataObjectSettings) {
            if (item == null) continue;

            if (current.isSelf() || item.isSelf()) {
                current = AuthDataSetting.Self;
                continue;
            }
            if (current.isDepartment() || item.isDepartment()) {
                current = AuthDataSetting.Department;
                continue;
            }
            if (current.isDepartmentAndLower() || item.isDepartmentAndLower()) {
                current = AuthDataSetting.DepartmentAndLower;
                continue;
            }
            if (current.isCustom() || item.isCustom()) {
                current = new AuthDataSetting();
                current.setCustom(true);
                if (current.getCustomData() == null) {
                    current.setCustomData(new ArrayList<>());
                }
                //合并数组
                List<String> customData = item.getCustomData();
                if (customData != null && customData.size() > 0) {
                    current.getCustomData().removeAll(customData);
                    current.getCustomData().addAll(customData);
                }
                continue;
            }
            if (current.isAll() || item.isAll()) {
                current = AuthDataSetting.All;
            }
        }
        return current;
    }

    /**
     * 是否未配置
     */
    @JsonIgnore
    public boolean isNone() {
        if (!this.isAll() && !this.isDepartment() && !this.isDepartmentAndLower()
                && !this.isSelf() && !this.isCustom()) {
            return true;
        }
        return false;
    }

    /**
     * 获取数据权限描述
     */
    @JsonIgnore
    public String description() {
        String dataName = "未配置";
        if (this.isSelf()) {
            dataName = "仅本人";
        } else if (this.isDepartment()) {
            dataName = "所在部门";
        } else if (this.isDepartmentAndLower()) {
            dataName = "所在部门及下属";
        } else if (this.isAll()) {
            dataName = "全部";
        } else if (this.isCustom()) {
            dataName = "自定义";
        }
        return dataName;
    }
}
